package com.six.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.bean.Staff;
import com.six.service.StaffService;
import com.six.service.impl.StaffServiceImpl;
import com.six.util.DeCodeUtil;
import com.six.util.JWTUtil;
import com.six.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("staff")
@CrossOrigin
public class StaffController {
    @Autowired
    StaffService staffService;

    @GetMapping("list")
    public Result getList() {
        List<Staff> list = staffService.list();
        return Result.success(list);
    }

    @GetMapping("page")
    public Result getPage(Page page) {
        Page result = staffService.page(page);
        return Result.success(result);
    }
    @PostMapping
    public Result add(@RequestBody Staff staff){
       staffService.save(staff);
        Staff staff1=staffService.getById(staff.getStaffid());
        return Result.success("保存成功",staff1);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){                      //注解 把两个id相关联
        boolean b=staffService.removeById(id);
        if(b){
            return Result.success("删除成功");
        }
        else {
            return Result.fail(201,"删除失败");
        }
    }
    @PutMapping
    public Result edit(@RequestBody Staff staff){
        boolean b=staffService.saveOrUpdate(staff);
        Staff staff1=staffService.getById(staff.getStaffid());
        if(b){
            return Result.success("保存成功",staff1);
        }
        else{
            return Result.fail(201,"修改失败");
        }
    }



}

